﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using System.Xml.Linq;

namespace TweetsTranslate.Helpers
{
    public class HelperMethods
    {
        public static List<SelectListItem> CreateSelectedList(List<string> languages)
        {
            List<SelectListItem> sliList = new List<SelectListItem>();
            foreach (string lang in languages)
            {
                SelectListItem sli = new SelectListItem();
                sli.Text = lang;
                sli.Value = lang;
                if (lang.Equals("en"))
                {
                    sli.Selected = true;
                }
                else
                {
                    sli.Selected = false;
                }
                sliList.Add(sli);
            }

            return sliList;
        }

        public static void SetSelectedLanguage(IEnumerable<SelectListItem> languageListFrom, string searchLanguage)
        {
            foreach (SelectListItem item in languageListFrom)
            {
                if (item.Value.Equals(searchLanguage))
                {
                    item.Selected = true;
                }
                else
                {
                    item.Selected = false;
                }
            }
        }

        public static string RequestCAPTCHA(ref List<string> currentCaptchaAnswers)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://api.textcaptcha.com/" +
                ConfigurationManager.AppSettings["captchaKey"]);
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            //Get Response
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


            XDocument document = XDocument.Load(myHttpWebResponse.GetResponseStream());
            string question = document.Descendants("question").First().Value;
            currentCaptchaAnswers = document.Descendants("answer").ToList().Select(x => x.Value).ToList();

            return question;
        }

        private static string GetMd5Hash(MD5 md5Hash, string input)
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}